<?php
/*
商品详情
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
//cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->setting;
$return['set']=$basic;
$return['setting']=$basic;
$return['pluginset']['footernav']=$this->setting['footernav4'];
if($_RQ['op']=='main'){
	model('jy_creditshop/order')->up_verify();
	$params=array('credit_type'=>4,'paymerchid'=>$_SESSION['onemerchid'],'merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$return['params']['type']=100;
	if(isset($_RQ['status'])){
		if($_RQ['status']!=100){
			$params['status']=$_RQ['status'];
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['type'])){
		if($_RQ['type']!=100){
			$params['type']=$_RQ['type'];
		}
		$return['params']['type']=$_RQ['type'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$params['mobile'];
		$_SESSION['mobile']=$_RQ['mobile'];
	}elseif($_SESSION['mobile']){
		$params['mobile']=$_SESSION['mobile'];
	}
	if(!$_SESSION['mobile'] && !$_SESSION['uid']){
		$return['list']=array();
		exi($return);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('jy_creditshop_log',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		$credit_types=credit_types(true);
		foreach($list as &$item){
			$item['createtime'] = date('Y-m-d H:i:s', $item['createtime']);
			$item['goods'] = pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			$item['isreply'] = $basic['isreply'];
			$item['verifycode'] = pdo_getcolumn('jy_creditshop_verify',array('uniacid'=>$_SESSION['uniacid'],'logid'=>$item['id']),'verifycode');
			$item['statustitle'] = $this->order_status[$item['status']];
			if($item['goods']['isverify']){
				if($item['status']==1){
					$item['statustitle'] = '待兑换';
				}
				if($item['status']==3){
					$item['statustitle'] = '已兑换';
				}
			}
			if($item['status']==1 && $log['addressid']){
				$item['statustitle'] = '待发货';
			}
			$item['verifyurl']=custom_url('jy_creditshop/verify.detail//verifycode='.$item['verifycode'],true);
			$item['credit_typetitle']=$credit_types[$item['credit_type']];
			if($item['paymerchid']){
				$item['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['paymerchid']),'merchname');
			}
		}
	}
	$return['list']=$list;
	set_html('title',$basic['title']?'参与记录-'.$basic['title']:'参与记录');
	$return['sharedata']=array(
		'title'=>$basic['share_title'],
		'desc'=>$basic['share_desc'],
		'link'=>custom_url('jy_guashop/creditshop.goods/',true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	exi($return);
}
//获取红包
if($_RQ['op']=='red'){
	$post=get_postdata();
	$logid=intval($_RQ['logid']);
	$log=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
	if (!($log)){
		exi('该订单不存在或已删除！','error');
	}

	if((2 < $log['status']) && (0 < $log['time_finish'])){
		exi('红包已领取！','error');
	}

	if($log['status'] < 2){
		exi('红包未满足领取条件！','error');
	}
	$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']));
	
	//计算商品红包金额
	//固定金额
	if($goods['packettype']==1){
		$money=$goods['grant2'];
	}
	//随机金额
	else{
		$money=rand($goods['minpacketmoney']*100,$goods['maxpacketmoney']*100)/100;//金额
	}
	if($goods['surplusmoney']<$money && $goods['surplusmoney']>1){
		$money=$goods['surplusmoney'];
	}
	if($goods['surplusmoney']-$money<$goods['packetlimit']){
		exi('红包金额已达到限定金额！请联系管理员！','error');
	}
	if($money <1){
		exi('红包金额不能小于1！','error');
	}
	$openid=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'openid');
	$params=array();
	$params['mch_billno'] = $this->config['mch_id'].date("Ymd",time()).date("His",time()).rand(1111,9999);
	$params['openid'] = $openid;
	$params['money'] = $money;
	$params['remark'] = $basic['red_remark']?:'积分兑换红包';
	$params['wishing'] = $basic['red_wishing'];
	$params['act_name'] = $basic['act_name'];
	$res=cfc('wechat_pay')->redpack($params);
	if($res['result_code'] == 'SUCCESS'){		
		$update['time_finish'] = time();
		$update['status'] = 3;
		pdo_update('jy_creditshop_log', $update, array('id' => $logid));
		$updategoods['surplusmoney'] = $goods['surplusmoney'] - $money;
		pdo_update('jy_creditshop_goods', $updategoods, array('id' => $log['goodsid']));
		exi('红包领取成功');
	}else{
		exi('红包领取失败，请联系管理员设置好参数！','error');
	}
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($order['money']>0){
		exi($order['pay_id']);
	}else{
		$params=array(
			'onemerchid'=>$_SESSION['onemerchid'],
		);
		model('jy_creditshop/order')->order_pay($order['id'],$params);
		exi('兑换成功！','','/log_list/');
	}
}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$update=array('status'=>3,'time_finish'=>TIMESTAMP);
	pdo_update('jy_creditshop_log',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('收货成功！');	
}
if($_RQ['op']=='rafund'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	model('jy_creditshop/order')->rafund($id);
	exi('退款成功！');	
}
if($_RQ['op']=='status1'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$update=array('status'=>1);
	pdo_update('jy_creditshop_log',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');	
}
//挂卖
if($_RQ['op']=='status8'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('gua')->create($id);
	exi('挂卖成功！');	
}
//取消挂卖
if($_RQ['op']=='status7'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('gua')->tuicreate($id);
	exi('取消成功！');	
}